﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：马毅 
    //
    // 文件名：ChildUserAdd.cs
    // 文件功能描述：企业注册
    // 创建标识：马毅2009-05-26
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Data;
using HZ.DrugTrade.Model.Enum;
namespace HZ.DrugTrade.Web
{
    public partial class RegCompany : BasePage
    {
        IBLL_Company company = Factory.BLL_Company();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Request.QueryString["companyid"] != null)
                {
                    Bind();
                    btnAdd.Text = "修改";
                    txtCompanyID.Enabled = false;
                    DllType.Enabled = false;
                }

            }
        }

        public void Bind()
        {
            CompanyInfo cominfo = new CompanyInfo();
            string companyid = Request.QueryString["companyid"].ToString();
            cominfo = company.GetCompanyInfoByID(companyid);
            txtCompanyID.Text = companyid;
            DllType.SelectedValue = cominfo.CompanyType;
            txtcompanyname.Text = cominfo.CompanyName;
            txtTelNo.Text = cominfo.TelNo;
            txtContactPerson.Text = cominfo.ContactPerson;
            txtCellNo.Text = cominfo.CellNo;
            txtLayMan.Text = cominfo.LayMan;
            txtAddress.Text = cominfo.Address;
            txtBusinessCertificate.Text = cominfo.BusinessCertificate;
            txtBusinessCertificateDate.Text = cominfo.BusinessCertificateDate.ToString();
            if (cominfo.Businessloogtime == 1)
            {
                this.ckBusinessCertificateDate.Checked = true;
                isshows.Visible = false;
            }
            txtFax.Text = cominfo.Fax;
            txtWebsite.Text = cominfo.Website;
            txtBusinessRange.Text = cominfo.BusinessRange;
            txtRemarks.Text = cominfo.Remarks;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string code = this.txtCode.Text;
            if (CurrentAccount.VerifyValidateCode(code))
            {

                string CompanyID = txtCompanyID.Text.Trim();
                if (!(CompanyID.ToLower().StartsWith("qp") || CompanyID.ToLower().StartsWith("j") || CompanyID.ToLower().StartsWith("s")))
                {
                    HZ.Web.WebUtil.Alert("检查企业编号，不符合规则！", this);

                    return;
                }else if (new HZ.Common.ConvertHelper(txtcompanyname.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtCellNo.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtContactPerson.Text.Trim()).String != string.Empty)
                {
                    CompanyInfo compinfo = new CompanyInfo();
                    //  compinfo.CompanyID = Guid.NewGuid().ToString();
                    compinfo.CompanyName = txtcompanyname.Text.Trim();
                    compinfo.TelNo = txtTelNo.Text.Trim();
                    compinfo.ContactPerson = txtContactPerson.Text.Trim();
                    compinfo.CellNo = txtCellNo.Text.Trim();
                    compinfo.LayMan = txtLayMan.Text.Trim();
                    compinfo.Address = txtAddress.Text.Trim();
                    compinfo.BusinessCertificate = txtBusinessCertificate.Text.Trim();
                    compinfo.BusinessCertificateDate = new HZ.Common.ConvertHelper(txtBusinessCertificateDate.Text.Trim()).ToDateTime;
                    compinfo.Website = txtWebsite.Text.Trim();
                    //compinfo.GSPCode = txtGSPCode.Text.Trim();
                    //compinfo.GSPTime = new HZ.Common.ConvertHelper(txtGSPTime.Text.Trim()).ToDateTime;
                    if (this.ckBusinessCertificateDate.Checked == true)
                    {
                        compinfo.Businessloogtime = 1;
                    }
                    else
                    {
                        compinfo.Businessloogtime = 0;
                    }
                    compinfo.BusinessRange = txtBusinessRange.Text.Trim();
                    compinfo.Remarks = txtRemarks.Text.Trim();
                    compinfo.IsSubmit = 0;
                    compinfo.CompanyID = txtCompanyID.Text.Trim().ToUpper();//企业编号
                    
                    //加企业的同时添加类型 zdc
                    string comType;
                    if (this.DllType.SelectedIndex != 0)
                    {
                        comType = this.DllType.SelectedValue;
                    }
                    else
                    {
                        this.Controls.Add(new LiteralControl("<script>alert('请选择企业类型');</script>"));
                        return;
                    }
                    if(btnAdd.Text == "修改")
                    {
                        int flag = company.UpdateCompanyInfoByCompanyId(compinfo);
                        if (flag == 1)
                        {
                            WebHint.ShowInfo("修改成功！", this.Request.RawUrl);
                        }
                        else
                        {
                            WebHint.ShowError("修改失败！", "");
                        }
                    }
                    else
                    {
                        int result = company.RegNewCompany(compinfo, comType);
                        if (result == 1)
                        {
                            WebHint.ShowInfo("提交成功！", this.Request.RawUrl);
                        }
                        else if (result == 2)
                        {
                            HZ.Web.WebUtil.Alert("企业信息已经存在！", this);
                        }
                        else
                        {
                            WebHint.ShowError("提交失败！", "");
                        }
                    }
                    //int result = company.RegNewCompany(compinfo);
                    
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请填写必填项！", this);
                }
            }
            else
            {
                this.txtCode.Text = "";
                HZ.Web.WebUtil.Alert("验证码不正确！", this);
            }
        }
        
       
        protected void DllType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string comID = "";
            string comID1 = "";
            IBLL_Company comInfo = Factory.BLL_Company();
            if (this.DllType.SelectedIndex==0)
            {
                this.txtCompanyID.Text = "";
            }
            if (DllType.SelectedIndex==1)
            {
                comID=comInfo.GetCompanySID();
                string id = comID.Substring(2);
                if (Convert.ToInt32(id) + 1 < 10)
                {
                    id = "000" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 100)
                {
                    id = "00" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 1000)
                {
                    id = "0" + (Convert.ToInt32(id) + 1).ToString();
                }
                else
                {
                    id = (Convert.ToInt32(id) + 1).ToString();
                }
                
                comID1="QS"+id.ToString();
                txtCompanyID.Text = comID1;
            }

            if (DllType.SelectedIndex == 2)
            {
                comID = comInfo.GetCompanyJID();
                string id = comID.Substring(2);
                if (Convert.ToInt32(id) + 1 < 10)
                {
                    id = "000" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 100)
                {
                    id = "00" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 1000)
                {
                    id = "0" + (Convert.ToInt32(id) + 1).ToString();
                }
                else
                {
                    id = (Convert.ToInt32(id) + 1).ToString();
                }
                comID1 = "QJ" + id.ToString();
                txtCompanyID.Text = comID1;
            }

            if (DllType.SelectedIndex == 3 || DllType.SelectedIndex == 4 || DllType.SelectedIndex == 5)
            {
                if (string.IsNullOrEmpty(comInfo.GetCompanyPID()))
                {
                    comID = "QP0000";
                }
                else
                {
                    comID = comInfo.GetCompanyPID();
                }
                string id = comID.Substring(2,4);
                if (Convert.ToInt32(id) + 1 < 10)
                {
                    id = "000" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 100)
                {
                    id = "00" + (Convert.ToInt32(id) + 1).ToString();
                }
                else if (Convert.ToInt32(id) + 1 < 1000)
                {
                    id = "0" + (Convert.ToInt32(id) + 1).ToString();
                }
                else
                {
                    id = (Convert.ToInt32(id) + 1).ToString();
                }
                comID1 = "QP" + id.ToString();
                txtCompanyID.Text = comID1;
            }
        }

        protected void ckBusinessCertificateDate_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckBusinessCertificateDate.Checked == true)
            {
                isshows.Visible = false;
                this.txtBusinessCertificateDate.Text = "2999-12-31";
            }
            else
            {
                isshows.Visible = true;
                this.txtBusinessCertificateDate.Text = "";
            }
        }


    }
}
